﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using QMSDAL;

namespace QuaManSys.OuterInfo
{
    public partial class OuterInfoQueryDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurUser"] == null)
            {
                Response.Redirect("~/Login.aspx");
            }
            lblQuaInfoProNo.Text = Request.QueryString["id"]; //修改为查询字符串
            fdbktime.Attributes.Add("ReadOnly", "True");
            lblCusRT.Attributes.Add("ReadOnly", "True");
            txtProDL.Attributes.Add("ReadOnly", "True");
            txtKeyWords.Attributes.Add("ReadOnly", "True");

            if (!IsPostBack)
            {
                initddlProNo();
                initResponDept();


                initQuaInfoTextBox();
                initDepartTextBox();
                initFeedBackTextBox();
            }
            this.Title = "外部质量信息查询";

        }

        #region display
        /// <summary>
        /// 把外部质量信息表中的数据显示出来
        /// </summary>
        private void initQuaInfoTextBox()
        {
            using (QuaManSysEntities context = new QuaManSysEntities())
            {
                var qualityInfo = from c in context.OuterQualityInfo
                                  where c.qualityInfoId == lblQuaInfoProNo.Text
                                  select c;
                txtSearch.Text = qualityInfo.Single().informationSource;
                txtImpLev.Text = qualityInfo.Single().levelOfImportance;
                ddlProNo.Text = qualityInfo.Single().projectNo;
                txtlblMacTyp.Text = GCBH.GetJXGCBH(ddlProNo.Text);
                txtlblMacNo.Text = GCBH.GetTFGCBH(ddlProNo.Text);
                txtComponent.Text = qualityInfo.Single().components;
                txtFaiTyp.Text = qualityInfo.Single().faultType;
                txtManufac.Text = qualityInfo.Single().producer;
                txtPjCls.Text = qualityInfo.Single().projectCategory;
                txtPjNam.Text = qualityInfo.Single().name;
                txtInfoDsc.Text = qualityInfo.Single().description;
                InfoCls.SelectedIndex = Convert.ToInt16(qualityInfo.Single().informationCategory);
                txtReqAdv.Text = qualityInfo.Single().recommendedTreatPlan;
                txtReqRsl.Text = qualityInfo.Single().processingResults;
                txtProCmm.Text = qualityInfo.Single().processingAssess;
                txtProDL.Text = qualityInfo.Single().deadline.ToString().Split(' ')[0].Replace('-', '/');

            }
        }

        /// <summary>
        /// 把责任部门和关注部门显示出来
        /// </summary>
        private void initDepartTextBox()
        {
            using (QuaManSysEntities context = new QuaManSysEntities())
            {
                var dept = from c in context.OuterResponsibleDepart
                           where c.qualityInfoId == lblQuaInfoProNo.Text
                           select c;
                string deptString = "";
                foreach (var item in dept)
                {
                    if (item.isResponDepart == 0)
                    {
                        if (deptString == "") deptString += item.department;
                        else deptString += "|" + item.department;
                    }
                    else
                    {
                        ResponDept.Text = item.department;
                    }
                }
                txtKeyWords.Text = deptString;
            }
        }

        /// <summary>
        /// 显示反馈表中的数据
        /// </summary>
        private void initFeedBackTextBox()
        {
            using (QuaManSysEntities context = new QuaManSysEntities())
            {
                var fbset = from c in context.OuterQualityInfoFeedback
                            where c.qualityInfoId == lblQuaInfoProNo.Text
                            select c;
                lblFdCom.Text = fbset.Single().feedbackUnit;
                txtFdPer.Text = fbset.Single().feedbackName;
                fdbktime.Text = fbset.Single().feedbackTime.ToString().Split(' ')[0].Replace('-', '/');
                ProcssComd.SelectedIndex = Convert.ToInt16(fbset.Single().processingDemands);
                lblCusCo.Text = fbset.Single().customerCompany;
                lblRelCo.Text = fbset.Single().relatedParty;
                lblCusNam.Text = fbset.Single().customerName;
                lblCusRol.Text = fbset.Single().customerRole;
                lblCusTel.Text = fbset.Single().customerPhone;
                lblCusRT.Text = fbset.Single().customerReqCloTime.ToString().Split(' ')[0].Replace('-', '/');
                CareLevel.SelectedIndex = Convert.ToInt16(fbset.Single().customerAttitude);
                TextBox4.Text = fbset.Single().cusomerOtherInfo;
                TextBox2.Text = fbset.Single().qualityInfoDes;
                TextBox3.Text = fbset.Single().solutionProposal;
                string s = fbset.Single().subsequentRequest;
                if (s.Length == 1)
                {
                    CustmReq.SelectedIndex = Convert.ToInt16(s);
                }
                else
                {
                    CustmReq.SelectedIndex = 3;
                    instruction.Text = s.Substring(2);
                }


            }
        }
        #endregion

        /*
         * multiView的下一步
         */
        protected void nextstep_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 1;
        }

        /*
         * multiView的上一步
         */
        protected void prestep_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 0;
        }

        /// <summary>
        /// 初始化工程编号下拉框
        /// </summary>
        private void initddlProNo()
        {
            DataSet myDs = GCBH.GetAllProjectNumbers();
            foreach (DataTable dt in myDs.Tables)
            {
                int count = 0;
                for (int curCol = 0; curCol < dt.Columns.Count; curCol++)
                {
                    for (int curRow = 0; curRow < dt.Columns.Count; curRow++)
                    {
                        ListItem li = new ListItem(dt.Rows[curRow][curCol].ToString());
                        ddlProNo.Items.Insert(count++, li);
                    }
                }
            }
            txtlblMacTyp.Text = GCBH.GetJXGCBH(ddlProNo.Text);
            txtlblMacNo.Text = GCBH.GetTFGCBH(ddlProNo.Text);
        }



        /// <summary>
        /// 初始化责任部门下拉框选项
        /// </summary>
        private void initResponDept()
        {
            DataSet ds = Department.GetAllDept();
            foreach (DataTable dt in ds.Tables)
            {
                ResponDept.Items.Clear();
                int count = 0;
                for (int curCol = 0; curCol < dt.Columns.Count; curCol++)
                {
                    for (int curRow = 0; curRow < dt.Rows.Count; curRow++)
                    {
                        if (dt.Rows[curRow][curCol].ToString() == ResponDept.Text) continue;
                        ListItem li = new ListItem(dt.Rows[curRow][curCol].ToString());
                        ResponDept.Items.Insert(count++, li);
                    }
                }
            }
        }


        /// <summary>
        /// 插入处理数据
        /// </summary>
        protected void InsertProinfo(object sender, EventArgs e)
        {
            TextBox txtProName = (TextBox)Repeater1.Controls[Repeater1.Controls.Count - 1].FindControl("txtProName");
            TextBox txtProDept = (TextBox)Repeater1.Controls[Repeater1.Controls.Count - 1].FindControl("txtProDept");
            TextBox txtProOpn = (TextBox)Repeater1.Controls[Repeater1.Controls.Count - 1].FindControl("txtProOpn");
            TextBox txtProDate = (TextBox)Repeater1.Controls[Repeater1.Controls.Count - 1].FindControl("txtProDate");

            using (var db = new QuaManSysEntities())
            {
                OuterQualityInfoPro Insertinfo = new OuterQualityInfoPro
                {
                    processerDepart = txtProDept.Text,
                    processerName = txtProName.Text,
                    processingDate = Convert.ToDateTime(txtProDate.Text),
                    opinion = txtProOpn.Text,
                    qualityInfoId = lblQuaInfoProNo.Text
                };

                db.OuterQualityInfoPro.Add(Insertinfo);
                db.SaveChanges();

            }

            Repeater1.DataSourceID = "EntityProcess";
            Repeater1.DataBind();
        }
    }
}